﻿#region Using statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

#endregion

namespace RhythmComet
{
    public interface ISongOverState : IGameState { }

    class SongOverState : GameState, ISongOverState
    {
        #region Fields and properties
        private Texture2D background;
        private Texture2D infoBox;
        private Vector2 boxPosition;
        private Vector2 backgroundPosition;
        private SpriteFont font;
        private SpriteFont smallFont;
        #endregion

        #region Initializing
        public SongOverState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(ISongOverState), this);
        }

        protected override void LoadContent()
        {
            background = game.Content.Load<Texture2D>("sprites/background");
            infoBox = game.Content.Load<Texture2D>("sprites/statisticsBackground");
            boxPosition = new Vector2((game.GraphicsDevice.Viewport.Width / 2 - infoBox.Width / 2),
                (game.GraphicsDevice.Viewport.Height / 2 - infoBox.Height / 2));
            font = game.Content.Load<SpriteFont>(@"fonts\GridExercise");
            smallFont = game.Content.Load<SpriteFont>(@"fonts\BerlinSmallCaps");
            backgroundPosition = new Vector2((game.GraphicsDevice.Viewport.Width / 2 - background.Width / 2),
                (game.GraphicsDevice.Viewport.Height / 2 - background.Height / 2));
        }
        #endregion

        #region Update and draw
        public override void Update(GameTime gameTime)
        {
            if (Input.GamePadState.Buttons.Back == ButtonState.Pressed)
                game.Exit();
#if !XBOX
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A) || Input.WasPressed(Keys.Enter))
#else
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A))
#endif
            {
                GameManager.PopState();
                GameManager.PushState(game.RetryMenuState.Value);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            string message;
            Vector2 position = new Vector2(boxPosition.X + 40, boxPosition.Y + 30) ;

            game.GraphicsDevice.Clear(Color.Black);

            game.SpriteBatch.Begin();

            game.SpriteBatch.Draw(background, backgroundPosition, Color.White);
            //game.SpriteBatch.DrawString(font, "Statistics", new Vector2(140, 40), Color.White);

            game.SpriteBatch.Draw(infoBox, boxPosition, Color.White);

            game.SpriteBatch.DrawString(smallFont, "Red", position, Color.White);
            message = string.Format("{0:00.00}%", game.Score.RedPercentage);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            position.Y += 20;
            game.SpriteBatch.DrawString(smallFont, "Green", position, Color.White);
            message = string.Format("{0:00.00}%", game.Score.GreenPercentage);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            position.Y += 20;
            game.SpriteBatch.DrawString(smallFont, "Blue", position, Color.White);
            message = string.Format("{0:00.00}%", game.Score.BluePercentage);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            position.Y += 20;
            game.SpriteBatch.DrawString(smallFont, "Yellow", position, Color.White);
            message = string.Format("{0:00.00}%", game.Score.YellowPercentage);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            position.Y += 20;
            game.SpriteBatch.DrawString(smallFont, "Faults", position, Color.White);
            message = string.Format("{0:00.00}%", game.Score.FaultPercentage);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            position.Y += 30;
            game.SpriteBatch.DrawString(smallFont, "Total Score", position, Color.White);
            message = string.Format("{0}", game.Score.TotalScore);
            game.SpriteBatch.DrawString(smallFont, message, new Vector2(position.X + 300, position.Y), Color.White);

            Vector2 offset = font.MeasureString("Continue") / 2;
            position = new Vector2(game.SpriteBatch.GraphicsDevice.Viewport.Width / 2, game.SpriteBatch.GraphicsDevice.Viewport.Height / 2 + 140);
            game.SpriteBatch.DrawString(font, "Continue", position - offset, Color.Red);

            game.SpriteBatch.End();

            base.Draw(gameTime);
        }
        #endregion
    }
}
